package cn.trigram.validation.annotation.validator;

import cn.trigram.validation.annotation.OracleNumberCheck;
import java.math.BigDecimal;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.StringUtils;

/**
 * OracleNumber校验器
 *
 * @version 1.0.0
 * @date 2022/08/04 09:11:06
 * @since 1.0.0
 */
public class OracleNumberForStringValidator extends AbstractOracleNumberValidator implements ConstraintValidator<OracleNumberCheck, String> {

    @Override
    public void initialize(OracleNumberCheck constraintAnnotation) {
        super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            // 空值放过
            return true;
        }
        //提前用BigDecimal检查是不是数字类型
        final BigDecimal fieldValue = new BigDecimal(value);
        return isValid(context, fieldValue);
    }
}